package com.doordu.soa.service.web.utils;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

/**
 * 提供对项目配置属性的访问.
 * 
 * @author kyler.tang
 *
 */
public class EnvironmentUtils implements EnvironmentAware {
	private static Logger log = LoggerFactory.getLogger(EnvironmentUtils.class);
	private static String _currProfile = null;
	private static Environment environment;

	@SuppressWarnings("static-access")
	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
		log.info("环境变量访问类初始化成功!");
	}

	public static Optional<String>  get(String key) {
		return Optional.ofNullable(environment.getProperty(key));
	}

	public static Environment getEnvironment() {
		return environment;
	}

	public static String getActiveProfile() {
		if (_currProfile != null)
			return _currProfile;
		
		if(environment == null){//测试环境直接返回dev
			return "dev";
		}
		
		String[] ps = environment.getActiveProfiles();
		if (ps == null || ps.length == 0) {
			return null;
		}
		
		_currProfile = ps[0];
		
		return _currProfile;
	}

}
